package com.mrx.java8.Date;

import org.junit.jupiter.api.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

public class TestNewDateApi {
    // 1、LocalDate localTime LocalDateTime
    @Test
    public void test1(){
        LocalDateTime ldt1 = LocalDateTime.now();
        System.out.println(ldt1);

        LocalDateTime ldt2 = LocalDateTime.of(2017,12,12,12,30,10);
        System.out.println(ldt2);


        LocalDateTime ldt3 = ldt1.plusYears(2);
        System.out.println(ldt3);

        LocalDateTime ldt4 = ldt1.minusMonths(13);
        System.out.println(ldt4);

        System.out.println(ldt1.getYear());
        System.out.println(ldt1.getMonthValue());
        System.out.println(ldt1.getDayOfMonth());
        System.out.println(ldt1.getHour());
        System.out.println(ldt1.getMinute());
        System.out.println(ldt1.getSecond());

    }


    // 2、Instant : 时间戳(以Unix元年 : 1970年1月1日00:00:00到某个时间之间的毫秒数)
    @Test
    public void test2(){
        Instant ins1 = Instant.now(); // 默认获取UTC时区
        System.out.println(ins1);

        OffsetDateTime ins2 = ins1.atOffset(ZoneOffset.ofHours(8));
        System.out.println(ins2);

        //毫秒值
        System.out.println(ins1.toEpochMilli());


        Instant ins3 = Instant.ofEpochSecond(1000);
        System.out.println(ins3);
    }


    // 3 、
    // Duration : 计算两个“时间”之间的间隔
    // Period : 计算两个“日期”之间的间隔

    @Test
    public void test3(){

        // 计算两个时间之间的间隔
        Instant ins1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant ins2 = Instant.now();

        Long millis = Duration.between(ins1,ins2).toMillis();
        System.out.println(millis);

        System.out.println("------------------------------------");

        LocalTime lt1 = LocalTime.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime lt2 = LocalTime.now();

        long millis2 =  Duration.between(lt1,lt2).toMillis();
        System.out.println(millis2);
    }

    // Period : 计算两个“日期”之间的间隔
    @Test
    public void test4(){
        LocalDate ld1 = LocalDate.of(2017,5,26);
        LocalDate ld2 = LocalDate.now();

        Period period = Period.between(ld1,ld2);

        System.out.println(period);
    }


    // 5、TemporalAdjuster
    @Test
    public void test5(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt2 = ldt.withDayOfMonth(10);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(ldt3);

        LocalDateTime ltd5 = ldt.with(t->{
            LocalDateTime ldt4 = (LocalDateTime) t;
            DayOfWeek dow = ldt4.getDayOfWeek();
            if (dow.equals(DayOfWeek.FRIDAY)){
                return ldt4.plusDays(3);
            }else if(dow.equals(DayOfWeek.SATURDAY)){
                return ldt4.plusDays(2);
            }else {
                return ldt4.plusDays(1);
            }
        });

        System.out.println(ltd5);
    }


    // 6、DateTimeFormatter : 格式化时间/日期
    @Test
    public void test6(){
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();
        String strDate = ldt.format(dtf);
        System.out.println(strDate);

        System.out.println("----------------------------");

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String strDate2 = ldt.format(dtf2);
        System.out.println(strDate2);

        System.out.println("----------------------------");

        LocalDateTime ldt2 = LocalDateTime.parse(strDate2,dtf2);
        System.out.println(ldt2);

    }

    //7 ZonedDate ZonedTime ZonedDateTime
    @Test
    public void test7(){
        Set<String> set = ZoneId.getAvailableZoneIds();
        set.forEach(System.out::println);
    }

    @Test
    public void test8(){
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime zdt = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zdt);
    }

}
